package com.kaifamiao.month03.day0311;

import java.util.Arrays;

/**
 * 分别创建出这 6 位同学，放到一个数组中
 * 对这六位同学进行排序，年龄小的在前面，如果年龄相同，按性别排（女士优先），性别也一样按姓名排序
 */
public class StudentDemo {

    public static void main(String[] args) {
        StudentDemo sd = new StudentDemo();
        Student student1 = new Student();
        student1.name = "张三";
        student1.age = 24;
        student1.gender = '男';
        student1.hobby = "打人";
        Student student2 = new Student();
        student2.name = "李四";
        student2.age = 23;
        student2.gender = '女';
        student2.hobby = "踢足球";
        Student student3 = new Student();
        student3.name = "王五";
        student3.age = 22;
        student3.gender = '女';
        student3.hobby = "打羽毛球";
        Student student4 = new Student();
        student4.name = "孙六";
        student4.age = 24;
        student4.gender = '男';
        student4.hobby = "打游戏";
        Student student5 = new Student();
        student5.name = "唯兮";
        student5.age = 21;
        student5.gender = '女';
        student5.hobby = "跑步";
        Student student6 = new Student();
        student6.name = "迪丽热巴";
        student6.age = 22;
        student6.gender = '女';
        student6.hobby = "健身";
        Student[] students = {student1, student2, student3, student4, student5, student6};
        Student[] studentsCopy = Arrays.copyOf(students,students.length);
        sd.sortAge(students);
        sd.introDemo(students);

    }

    /**
     * 对这六位同学进行排序，年龄小的在前面 如果年龄相同，按性别排（女士优先）
     * @param s 需要排序的数组
     */
    public void sortAge(Student[] s){
        for(int i = 0; i < s.length - 1; i ++){
            for (int j = 0; j < s.length - 1 - i; j ++) {
                if(s[j].age > s[j + 1].age){
                    Student temp =s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = temp;
                }else if (s[j].age == s[j + 1].age){
                    if(s[j].gender == '男' && s[j + 1].gender == '女'){
                        Student temp =s[j];
                        s[j] = s[j + 1];
                        s[j + 1] = temp;
                    }else if(s[j].gender == s[j + 1].gender) {
                        if(s[j].name.compareTo(s[j + 1].name) > 0){
                            Student temp =s[j];
                            s[j] = s[j + 1];
                            s[j + 1] = temp;
                        }
                    }
                }
            }
        }
    }

    /**
     * 分别自我介绍
     * @param s 需要介绍的对象数组
     */
    public void introDemo(Student[] s){
        for (Student student : s) {
            student.intro();
        }
    }

}
